package com.egoo.sys.controller;

import com.egoo.sys.cache.CachePool;
import com.egoo.sys.common.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 缓存控制器
*@Author Egoo
*@Date 2022/6/19 15:59
*/
@Api(description = "缓存管理")
@RestController
@RequestMapping("cache")
public class  CacheController {




    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 查询所有缓存
     * @return
     */
    @ApiOperation(value = "查询所有缓存",notes = "查询所有缓存")
    @RequestMapping(value = "loadAllCache",method = RequestMethod.GET)
    public DataGridView loadAllCache(){
        RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
        List<Object> list = new ArrayList<>();
        Set keys = redisTemplate.keys("*");
        for (Object key : keys) {
            list.add(new CacheBean((String)key,redisUtil.get((String) key)));
        }
        return new DataGridView(list);
    }

    /**
     * 删除缓存
     * @param key
     * @return
     */
    @RequestMapping("deleteCache")
    public ResultObj deleteCache(String key){
        CachePool.removeCacheByKey(key);
        return ResultObj.DELETE_SUCCESS;
    }

    /**
     * 清空所有缓存
     * @return
     */
    @RequestMapping("removeAllCache")
    public ResultObj removeAllCache(){
        CachePool.removeAll();
        return ResultObj.DELETE_SUCCESS;
    }

    /**
     * 同步缓存
     * @return
     */
    @RequestMapping("syncCache")
    public ResultObj syncCache(){
        CachePool.syncData();
        return ResultObj.SYNCCACHE_SUCCESS;
    }

}
